<template>
	<view>
			<uni-pay ref="pay" :adpid="adpid" return-url="/pages/order-detail/order-detail" logo="/static/logo.png" @success="onSuccess" @create="onCreate"></uni-pay>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				openid:'',
				options:{}
			}
		},
	async onReady(options={}) {
			var url = window.location.href ;             //获取当前url
		
			var dz_url = url.split('#')[0];                //获取#/之前的字符串
			var cs = dz_url.split('?')[1];                //获取?之后的参数字符串
			if(cs==undefined){
				await this.getWeiXinJsCode('snsapi_base')
			}else{
				var cs_arr = cs.split('&');                    //参数字符串分割为数组
				var cs={};
				for(var i=0;i<cs_arr.length;i++){         //遍历数组，拿到json对象
				cs[cs_arr[i].split('=')[0]] = cs_arr[i].split('=')[1]
				}
				options.code=cs.code
				options.state=cs.state
			}
	if (options.code && options.state) {
		// 获取微信公众号的openid
		setTimeout(() => {
			this.getOpenid({
				provider: "wxpay",
				code: options.code
			});
		}, 200);
		
	}
	
		},
		methods: {
			// 获取公众号code
			async getWeiXinJsCode(scope="snsapi_base") {
				let res = await this.$refs.pay.getProviderAppId({
					provider: "wxpay",
					provider_pay_type: "jsapi"
				});
				if (res.appid) {
					let appid = res.appid;
					let redirect_uri = encodeURIComponent(window.location.href.split("?")[0]);
					let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&response_type=code&scope=${scope}&redirect_uri=${redirect_uri}&state=STATE#wechat_redirect`;
					window.location.href = url;
				}
			 
			},
			// 获取公众号openid
			async getOpenid(data) {
				let res = await this.$refs.pay.getOpenid(data);
				if (res) {
					let openid=res.openid
					let url=`https://iot.ixingyue.com/weixin?openid=${openid}/#/pages/scan/scan`
					window.location.href = url;
					return
				
				}
			}
		}
		
	
	}
</script>

<style>

</style>
